<template>
  <div class="dict-manage-container">
    <!-- 左侧：字典列表区域 -->
    <div class="dict-left">
      <div class="dict-header">
        <el-input
            v-model="searchKeyword"
            placeholder="搜索字典"
            clearable
            size="small"
        />
        <el-button
            type="primary"
            icon="Plus"
            size="small"
            @click="handleAddDict"
        >
          新增
        </el-button>
      </div>
      <el-scrollbar class="dict-scroll">
        <div
            @click="clickId=item.id"
            class="dict-item"
            :class="{ 'dict-item-click': clickId === item.id }"
            v-for="item in filteredDicts"
            :key="item.id"
            @mouseenter="hoveredId = item.id"
            @mouseleave="hoveredId = null"
        >
          <span class="dict-name">{{ item.dictName }}</span>
          <span class="dict-right">
            <template v-if="hoveredId === item.id">
              <el-button :icon="Edit" size="small"
                         :class="{'icon-hover': hoveredId === item.id}"
                         @click.stop="handleEditDict(item)">
              </el-button>
              <el-button :icon="Delete"
                         :disabled="item.dictType==='system'"
                         size="small"
                         :class="{'icon-hover': hoveredId === item.id}"
                         @click.stop="handleDeleteDict(item.id)">
              </el-button>
            </template>
            <template v-else>
              <span class="dict-code">{{ item.dictCode }}</span>
            </template>
          </span>
        </div>
      </el-scrollbar>
    </div>
    <div class="dict-right">
      <itemList ref="itemListRef" :dict-id="clickId" class="itemList"></itemList>
    </div>
    <itemDia ref="itemDiaRef" @get-list="getList"></itemDia>
  </div>
</template>

<script setup>
import {ref, computed} from 'vue'
import {Edit, Delete} from '@element-plus/icons-vue'
import itemList from './dictList.vue'
import itemDia from './itemDia.vue'

const httpService = inject('httpService')

//字典项ref
const itemListRef = ref(null);

//定义字典编辑弹框ref
const itemDiaRef = ref(null);


// 字典数据
const dictList = ref([])

//当前选中的字典id
const clickId = ref(null)

//当前移动到的字典id
const hoveredId = ref(null)
//过滤的key
const searchKeyword = ref('')

// 过滤字典
const filteredDicts = computed(() => {
  const keyword = searchKeyword.value.trim().toLowerCase()
  if (!keyword) return dictList.value
  return dictList.value.filter(
      item =>
          item.dictName.toLowerCase().includes(keyword) ||
          item.dictCode.toLowerCase().includes(keyword)
  )
})

// 操作方法
const handleAddDict = () => {
  itemDiaRef.value.openDia(null, "新 增");

}

const handleEditDict = item => {
  itemDiaRef.value.openDia(item, "编 辑");
}

/**
 * 删除字典项
 * @param id
 */
const handleDeleteDict = (id) => {
  ElMessageBox.confirm('是否确认删除？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    httpService.del(`/sys-dict/delete-by-id/${id}`).then(() => {
      ElMessage.success('删除成功')
      getList()
    })
  })
}
//获取所有字典
const getList = () => {
  httpService.get("/sys-dict/findList").then(res => {
    dictList.value = res
  })
}

/**
 * 数据初始化
 */
(() => {
  getList();
})();


</script>

<style scoped>
.dict-manage-container {
  display: flex;
  height: 100vh;
  /* 左侧区域 */

  .dict-left {
    width: 20%;
    min-width: 200px;
    border-right: 1px solid #eee;
    display: flex;
    flex-direction: column;
    padding: 12px;
    box-sizing: border-box;
    /* 头部操作栏 */

    .dict-header {
      display: flex;
      gap: 8px;
      margin-bottom: 10px;
    }

    /* 滚动区域 */

    .dict-scroll {
      flex: 1;
      overflow: auto;
      /* 字典项 */

      .dict-item {
        display: flex;
        justify-content: space-between;

        border-bottom: 1px solid #f2f2f2;
        align-items: center;
        transition: background 0.2s;
        cursor: pointer;

        .dict-name {
          flex: 1;
          font-size: 14px;
          color: #333;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .dict-code {
          font-size: 12px;
          color: #999;
        }
      }

      .dict-item:hover {
        background: #eaeefe;
      }

      .dict-item-click {
        background: #eaeefe;
      }
    }

  }

  .dict-right {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: flex-end;
    min-width: 60px;
    flex: 1;
    padding: 16px;
    overflow: auto;

    .itemList {
      width: 100%;
    }

    .icon-hover:hover {
      color: #29AAE3;
    }
  }
}


</style>
